<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$config = array(
		//
		'cname' => 'item',
		
		//
		'class_file' => 'module/form/form.class.php',
		'class_name' => 'FormBase',
		'class_param' => array('_table_status_usable' => false),
		
		//
		'inject' => array(
				'categories_options' => array(
									'type' => 'cm',
									'class_file' => 'module/form/form.category.class.php', 
									'class_name' => 'FormCategoryBase',
									'method' => 'getCategoriesFormat',
									'params' => array('', 0, false),
										),
					),
		
		//
		'tpl_type' => 'admin',
		'tpl_file' => 'pagelayout/pagelayout_admin_form.tpl',
		
		//
		'validate_file' => '',
		'validate_class' => 'CommonValidate',
		'validate_fields' => array('field' => 'name', 'valid' => 'null'),
			);

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.control.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.content.control.class.php');

//
class _ControlItemContentBase extends ControlItemContentBase {
	
	//constructor
	function _ControlItemContentBase($config = '') {
		parent::ControlItemContentBase($config);
		
		//
		if( $this->feObj->operation == 'X' ) {
			$this->actionHandler = 'doXActionHandler';
		}
		if( $this->feObj->action == 'addformitem' ) {
			$this->actionHandler = 'doAddFormItemActionHandler';
		} elseif( $this->feObj->action == 'updateformitem' ) {
			$this->actionHandler = 'doUpdateFormItemActionHandler';
		} elseif( $this->feObj->action == 'updateformitems' ) {
			$this->actionHandler = 'doUpdateFormItemsActionHandler';
		}
		//
		if( $this->feObj->operation == 'formdetail' ) {
			$this->displayHandler = 'doFormDetailDisplayHandler';
		} elseif( $this->feObj->operation == 'formitems' ) {
			$this->displayHandler = 'doFormItemsDisplayHandler';
		} elseif( $this->feObj->operation == 'formpreview' ) {
			$this->displayHandler = 'doFormPreviewDisplayHandler';
		} elseif( $this->feObj->operation == 'help' ) {
			$this->displayHandler = 'doHelpDisplayHandler';
		}
	}
	
	//methods
	function doActionHandler() {
		echo 'doActionHandler';
		exit;
	}
	
	function doXActionHandler($obj) {
		
		_dms(':admin:success');
		$this->_redirect('', array('op'), 's');
	}
	
	function doAddFormItemActionHandler($obj) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.class.php');
		$mObj = new FormItemBase();
		$mObj->format = true;
		
		//
		$data_array = $this->_doFilter($_POST, 'add');
		$data_array['_fid'] = $_POST['fid'];
		$mObj->addItem($data_array);
		
		_dms(':admin:success');
		$this->_redirect('', '', 's');
	}
	
	function doUpdateFormItemActionHandler($obj) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.class.php');
		$mObj = new FormItemBase();
		$mObj->format = true;
		
		//
		
		_dms(':admin:success');
		$this->_redirect('', array('op'), 's');
	}
	
	function doUpdateFormItemsActionHandler($obj) {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.class.php');
		$mObj = new FormItemBase();
		$mObj->format = true;
		
		//
		
		_dms(':admin:success');
		$this->_redirect('', array('op'), 's');
	}
	
	//
	function doDisplayHandler() {
		echo 'doDisplayHandler';
		exit;
	}
	
	function doFormDetailDisplayHandler($obj) {
		$item = $this->modelObj->getItem($_GET['id'], 'id');
		
		//
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.class.php');
		$mObj = new FormItemBase();
		$mObj->format = true;
		$mObj->owner = $item['id'];
		$items = $mObj->getItems('', -1, false);
		
		//
		$this->feObj->content_wrap['mainpart'] = 'formdetail';
		$this->feObj->content_wrap['item'] = $item;
		$this->feObj->content_wrap['items'] = $items;
	}
	
	function doFormItemsDisplayHandler($obj) {
		$item = $this->modelObj->getItem($_GET['id'], 'id');
		
		//
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.class.php');
		$mObj = new FormItemBase();
		$mObj->format = true;
		$mObj->owner = $item['id'];
		$items = $mObj->getItems('', -1, false);
		
		//
		$this->feObj->content_wrap['mainpart'] = 'formitems';
		$this->feObj->content_wrap['item'] = $item;
		$this->feObj->content_wrap['items'] = $items;
	}
	
	function doFormPreviewDisplayHandler($obj) {
		$item = $this->modelObj->getItem($_GET['id'], 'id');
		
		//
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.item.class.php');
		$mObj = new FormItemBase();
		$mObj->format = true;
		$mObj->owner = $item['id'];
		$items = $mObj->getItems('', 0, false);
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			if( $items[$i]['type'] == 'select' || $items[$i]['type'] == 'radio' || $items[$i]['type'] == 'checkbox' ) {
				$items[$i]['value'] = util_text_split($items[$i]['value'], true);	
			}
		}
		
		//
		$this->feObj->content_wrap['mainpart'] = 'formpreview';
		$this->feObj->content_wrap['item'] = $item;
		$this->feObj->content_wrap['items'] = $items;
	}
	
	function doHelpDisplayHandler($obj) {
		$this->feObj->content_wrap['mainpart'] = 'help';
	}
	
	//
	function _doAfter() {
		
	}
}

//
$cntlObj = new _ControlItemContentBase($config);
$cntlObj->process();
//
?>